package ferp.core.calc.scorer;

import ferp.core.calc.Calculator;
import ferp.core.game.Game;
import ferp.core.game.Settings;
import ferp.core.player.Player;

import java.util.LinkedList;
import java.util.List;

/**
 * User: olegs
 * Date: 12/3/13 3:15 PM
 */
public class PassingRostov extends Passing
{
  public static final PassingRostov instance = new PassingRostov();

  @Override
  protected void update(Game g, Settings settings, Calculator c)
  {
    // find players with minimum number of tricks
    List<Player> min = new LinkedList<Player>();
    List<Player> other = new LinkedList<Player>();
    int minTricks = Game.MAXIMUM_TRICKS;

    for (Player p : g.players())
    {
      // in case player took no tricks write cost of 6 tricking game into his pool
      if (g.getTricks(p.id()) == 0)
        c.writeToPool(g, settings, p, c.getPlayCostSuccess(6));

      if (g.getTricks(p.id()) <= minTricks)
      {
        if (g.getTricks(p.id()) < minTricks)
        {
          other.addAll(min);
          min.clear();
        }

        min.add(p);
        minTricks = g.getTricks(p.id());
      }
      else
        other.add(p);
    }

    // write punish whists
    for (Player from : min)
      for (Player to : other)
        c.writeToWhists(g, from, to, Settings.ROSTOV_PASSING_TRICK_COST * g.getTricks(to.id()) / min.size());
  }
}
